var city = require('../../utils/city.js');
var fenzhan = require('../../utils/fenzhan.js');
var app = getApp()
Page({
  data: {
    searchLetter: [],
    showLetter: "",
    winHeight: 0,
    // tHeight: 0,
    // bHeight: 0,
    cityList: [],
    isShowLetter: false,
    scrollTop: 0,//置顶高度
    scrollTopId: '',//置顶id
    city: "",
    arealist:[],
    hotcityList: [{ cityCode: 110100, city: '北京市' }, { cityCode: 410100, city: '郑州市' }, { cityCode: 411600, city:'周口市'}, { cityCode: 320100, city: '南京市' },{ cityCode: 610100, city: '西安市' }, { cityCode: 510100, city: '成都市' },]
  },
  onLoad: function () {
    // 生命周期函数--监听页面加载
    var searchLetter = city.searchLetter;
    var cityList = city.cityList();
    var sysInfo = wx.getSystemInfoSync();
    var winHeight = sysInfo.windowHeight;
    var itemH = winHeight / searchLetter.length;
    var tempObj = [];
    for (var i = 0; i < searchLetter.length; i++) {
      var temp = {};
      temp.name = searchLetter[i];
      temp.tHeight = i * itemH;
      temp.bHeight = (i + 1) * itemH;
      tempObj.push(temp)
    }
    // console.log(cityList)
    this.setData({
      winHeight: winHeight,
      itemH: itemH,
      searchLetter: tempObj,
      cityList: cityList
    })
  },
  
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  onShow: function () {
    // 生命周期函数--监听页面显示
    var city = getApp().globalData.city;
    this.setData({city:city});
  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数

  },
  clickLetter: function (e) {
    // console.log(e.currentTarget.dataset.letter)
    var showLetter = e.currentTarget.dataset.letter;
    this.setData({
      showLetter: showLetter,
      isShowLetter: true,
      scrollTopId: showLetter,
    })
    var that = this;
    setTimeout(function () {
      that.setData({
        isShowLetter: false
      })
    }, 1000)
  },
  //选择城市
  getArea:function(id){
    // console.log(id)
    let that = this
    let cityList = fenzhan.cityList
    // console.log(cityList)
      for(let i = 0; i < cityList.length; i++){
        for (let j = 0; j < cityList[i].city.length; j++){
          if(id == cityList[i].city[j].id){
            // console.log(cityList[i].city[j].qu)
            that.setData({
              arealist: cityList[i].city[j].qu
            })
          }
        }
      }
  },
  changecity:function(e){
    let id = e.currentTarget.dataset.id
    let city = e.currentTarget.dataset.city
      // console.log(city)
    getApp().globalData.site_id = id
    getApp().globalData.city = city
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
    })
  },
  bindCity: function (e) {
    let code = e.currentTarget.dataset.city
    let id = e.currentTarget.dataset.citycode
    this.getArea(id);
    // console.log(id)
    // console.log(code)
    this.setData({ city: e.currentTarget.dataset.city })
    getApp().globalData.city = e.currentTarget.dataset.city;
    getApp().globalData.site_id = id
    this.hotCity()
  },
  //选择热门城市
  bindHotCity: function (e) {
    
    let id = e.currentTarget.dataset.citycode
    this.getArea(id);
    // console.log(id)
    this.setData({
      city: e.currentTarget.dataset.city
    })
    getApp().globalData.city = e.currentTarget.dataset.city;
    getApp().globalData.site_id = id
    
    // wx.navigateBack({
    //   delta: 1, // 回退前 delta(默认为1) 页面
    //   success: function(res){
    //     // success
    //   }
    // })
  },
  //点击热门城市回到顶部
  hotCity: function () {
    this.setData({
      scrollTop: 0,
    })
  }
})